import matplotlib.pyplot as plt
import numpy as np
from colour import XYZ_to_xyY, xyY_to_XYZ, RGB_to_XYZ, XYZ_to_sRGB
import cv2
from matplotlib.widgets import Slider

sRGB_xy = [(0.64,0.33),(0.3,0.6),(0.15,0.06)]

led_xy = [(0.7006,0.2993),(0.2658,0.7243),(0.1440,0.0297)]

src_pts=np.float32(sRGB_xy)
dst_pts=np.float32(led_xy)
M = cv2.getAffineTransform(src_pts,dst_pts)

def RGB2xyY(rgb):
    XYZ = RGB_to_XYZ(rgb, 'sRGB')
    Yxy = XYZ_to_xyY(XYZ)
    return (Yxy[0],Yxy[1],Yxy[2])

# 定义标准RGB颜色集
colors = [
    (1, 0, 0),    # Red
    (0, 1, 0),    # Green
    (0, 0, 1),    # Blue
    (1, 1, 0),    # Yellow
    (0, 1, 1),    # Cyan
    (1, 0, 1),    # Magenta
    (1, 1, 1),    # White
    (0, 0, 0),    # Black
    (0.5, 0.5, 0.5), # Gray
    (1, 0.5, 0),  # Orange
    (0.5, 0, 0.5), # Purple
    (0, 0.5, 0.5), # Teal
    (0.5, 0.5, 0), # Olive
    (0.5, 0, 0),   # Maroon
    (0, 0.5, 0),   # Lime
    (0, 0, 0.5),   # Navy
    (0.5, 0.25, 0), # Brown
    (0.25, 0.5, 0.5), # Turquoise
    (0.75, 0.75, 0.75), # Light Gray
    (0.25, 0.25, 0.25)  # Dark Gray
]

colors_xyY = [RGB2xyY(c) for c in colors]
colors_xyY = np.array(colors_xyY,dtype='float32')
print(colors_xyY)

colors_xy = np.delete(colors_xyY,2,1)

colors_Y=np.delete(colors_xyY,[0,1],1)
colors_Y=np.array(colors_Y,dtype='float32')

print(colors_xy)

def TransForm(xy):
    xy = np.array(xy,dtype='float32')
    xy_reshape = xy.reshape(-1,1,2)
    rgb = cv2.transform(xy_reshape,M)
    print("-->")
    print(rgb)
    return rgb

#led_colors_xy=[TransForm(c) for c in colors_xy]
led_colors_xy=TransForm(colors_xy)
led_colors_xy=np.array(led_colors_xy)
led_colors_xy=led_colors_xy.reshape(-1,2)
led_colors_xyY=np.hstack((led_colors_xy,colors_Y))

print(led_colors_xyY)

def xyY_to_RGB(xyY):
    XYZ = xyY_to_XYZ(np.array(xyY))
    rgb = XYZ_to_sRGB(XYZ)
    return rgb

led_rgb=xyY_to_RGB(led_colors_xyY)

#for xy in led_colors_xy:
#    rgb = xy_to_RGB(xy,)
led_rgb=np.clip(led_rgb,0,1)

def drawColorCard(colors):
    # 定义色卡的大小
    num_colors = len(colors)
    rows = int(np.ceil(np.sqrt(num_colors)))
    cols = int(np.ceil(num_colors / rows))

    # 创建一个空白图像
    image = np.zeros((rows * 100, cols * 100, 3), dtype=np.uint8)

    # 填充图像以创建色卡
    for idx, color in enumerate(colors):
        row = idx // cols
        col = idx % cols
        image[row*100:(row+1)*100, col*100:(col+1)*100] = (np.array(color) * 255).astype(np.uint8)

    return image

image_led=drawColorCard(led_rgb)
image_rgb=drawColorCard(colors)



# 创建图形和轴
fig, ax = plt.subplots(ncols=2,figsize=(10,5))
plt.subplots_adjust(bottom=0.25)  # 调整子图位置以给滑动条留出空间
# fig.set_size_inches(10, 5)  # 设置图形的宽度是高度的一倍

#ax.xaxis.set(0)
#ax.yaxis.set(0)

ax[0].set_aspect('equal')
ax[0].set_xlim(0, 1)
ax[0].set_ylim(0, 1)

ax[1].set_aspect('equal')
ax[1].set_xlim(0, 1)
ax[1].set_ylim(0, 1)


# 显示图像
ax[0].imshow(
    image_rgb,
    origin='lower',
    extent=[0, 1, 0, 1],
    interpolation='bilinear')

ax[1].imshow(
    image_led,
    origin='lower',
    extent=[0, 1, 0, 1],
    interpolation='bilinear')

ax[0].axis('off')  # 关闭坐标轴
ax[0].set_title('Standard RGB Color Chart')

ax[1].axis('off')  # 关闭坐标轴
ax[1].set_title('Standard Trans RGB Color Chart')


# 定义滑动条更新函数
def update(val):
    print(r_spectrum.val)
    print(g_spectrum.val)
    print(b_spectrum.val)
    fig.canvas.draw_idle()


led_name = ["R","G","B"]
led_spectrum = [[620,625],[515,545],[455,475]]  # R G B
led_mcd = [[11,25],[45,95],[455,475]] # R G B

def spectrum_slider(index,name,min,max,valinit = 0,valinterval = 5):
    ax = plt.axes([0.1 + index * 0.3, 0.15, 0.2, 0.05])
    return Slider(ax, name, min, max, valinit,valinterval)

r_spectrum = spectrum_slider(0,led_name[0],led_spectrum[0][0],led_spectrum[0][1])
g_spectrum = spectrum_slider(1,led_name[1],led_spectrum[1][0],led_spectrum[1][1])
b_spectrum = spectrum_slider(2,led_name[2],led_spectrum[2][0],led_spectrum[2][1])

r_spectrum.on_changed(update)
g_spectrum.on_changed(update)
b_spectrum.on_changed(update)

plt.show()
